# -*- coding:utf-8 -*-
from database.db_business import DbBusiness
from common.my_time import MyTime
from common.my_baidu import MyBaidu
from common.attr_v3 import AttrV3
from bs4 import BeautifulSoup
from common.my_file import MyFile
import glob
import jieba
import re
import threading
import hashlib
import urllib.parse
import time
import os
from common.market_data import MarketData


'''
网络数据
'''

class BaiduHangkong:
    def __init__(self):
        self.attr_v3 = AttrV3()
        self.business = DbBusiness()
        self.market = MarketData()
        self.log_file = open('./out/' + os.path.split(__file__)[-1].split(".")[0] + ".log", 'a', encoding = 'utf-8')
        MyFile.wrtie_log(self.log_file, "开始")
        self.all = 0
        self.error = 0

    def __del__(self):
        MyFile.wrtie_log(self.log_file, "结束")
        self.log_file.close()

    def check_site(self, site_name):
        site_lists = ["站长之家","中关村在线","新浪","搜狐","东方财富网","同花顺","网易","金融界","证券之星","每日经济新闻","中国财经信息网","凤凰","和讯","中国经济网","格隆汇","中国网","人民资讯","中证网","第一财经","证券时报","华夏时报","中华网","经济观察报","人民网","中国民航网"]
        for d in site_lists:
            if re.search(d, site_name):
                return True
        print(site_name)
        return False
    
    def get_data(self, word):
        datas = ["山东航空","荷兰航空","卡塔尔航空","澳洲航空","首都航空","阿联酋国际航空","马来西亚航空","阿联酋航空","天津航空","美国航空","奥凯航空","春秋航空","法国航空","联合航空","汉莎航空","英国航空","达美航空","芬兰航空","加拿大航空","大韩航空","河北航空","吉祥航空","新加坡航空","厦门航空","长荣航空","中华航空","港龙航空","深圳航空","海南航空","国泰航空","南方航空","四川航空","东方航空","中国国航","韩亚航空","澳门航空","新西兰航空","全日空","美国西南航空","日本航空"]
        end_date = MyTime.forward_relative_date(60)
        check_title = [word]
        for k in datas:
            next_page = True
            words = [k, word]
            for i in range(1,10):
                #time.sleep(1)
                if next_page == False:
                    break
                url = MyBaidu.get_url_order_by_time(words, i)
                self.all = self.all + 1
                try:
                    proxy = self.business.query_proxy()
                    res = MyBaidu.get_baidu_data_by_proxy(url, proxy)
                    for r in res:
                        data_date = MyBaidu.calc_date(r["date"])
                        if data_date < end_date:
                            next_page = False
                            break
                        #if self.check_site(r["site"]) == False:
                        #    continue
                        if MyBaidu.check_title(r["title"], [[k], check_title]) == False:
                            continue
                        data_title = r["title"]
                        data_site = r["site"]
                        data_url = r["url"]
                        print(r["date"] + data_date)
                        self.market.add_market_data(data_date, 2304, data_title, data_url, data_site)
                except Exception as e:
                    i = i - 1
                    self.error = self.error + 1
                    pass

def hangkong_api():
    d = BaiduHangkong()
    d.get_data("运费")
    d.get_data("运价")
    d.get_data("运输量")
    d.get_data("货量")
    d.get_data("营收")
    d.get_data("航班")
    d.get_data("计划")
    d.get_data("宣布")
    d.get_data("恢复")
    d.get_data("客运量")
    d.get_data("货运量")



if __name__ == '__main__':
    hangkong_api()
